﻿using Beyova.JPush;
using Beyova.JPush.V3;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

namespace SocketLib
{
    public static class JPushHelper
    {
        /// <summary>
        /// JPushV3
        /// </summary>
        /// <param name="RegistrationID"></param>

        /// <param name="content"></param>
        /// <param name="isType">0  全体 1个人</param>
        /// <returns></returns>
        public static bool JPush(string RegistrationID, string title, string content, int isType = 1)//
        {
            try
            {





                string appKey = "4130ecd03a0fe97f24270663"; // Your App Key from JPush
                string masterSecret = "57a6713cd80a4f16578fd462"; // Your Master Secret from JPush

                Dictionary<string, Object> customizedValues = new Dictionary<string, Object>();
                customizedValues.Add("CK1", "CV1");
                customizedValues.Add("CK2", "CV2");


                JPushClientV3 client = new JPushClientV3(appKey, masterSecret);
                Audience audience = new Audience();
                // In JPush V3, tag can be multiple added with different values.
                // In following code, it is to send push to those who are in ((Tag1 AND Tag2) AND (Tag3 OR Tag4))
                // If you want to send to all, please use: audience.Add(PushTypeV3.Broadcast, null);
                //audience.Add(PushTypeV3.ByTagWithinAnd, new List<string>(new string[] { "Tag1", "Tag2" }));
                //audience.Add(PushTypeV3.ByTagWithinOr, new List<string>(new string[] { "Tag3", "Tag4" }));
                if (isType == 1)
                {
                    audience.Add(PushTypeV3.ByRegistrationId, new List<string>(new string[] { RegistrationID }));
                }
                else
                {
                    audience.Add(PushTypeV3.Broadcast, null);
                }
                // In JPush V3, Notification would not be display on screen, it would be transferred to app instead.
                // And different platform can provide different notification data.
                Notification notification = new Notification
                {
                    AndroidNotification = new AndroidNotificationParameters
                    {
                        Title = title,
                        Alert = content,
                        CustomizedValues = customizedValues
                    },
                    iOSNotification = new iOSNotificationParameters
                    {
                        Badge = 1,
                        Alert = content,
                        Sound = "happy",
                        CustomizedValues = customizedValues
                    }
                };


                var response = client.SendPushMessage(new PushMessageRequestV3
                {
                    Audience = audience,
                    Platform = PushPlatform.AndroidAndiOS,
                    IsTestEnvironment = true,
                    AppMessage = new AppMessage
                    {
                        Content = content,
                        CustomizedValue = customizedValues
                    },
                    Notification = notification
                });


                return "Succced" == response.ResponseCode.ToString();
            }
            catch (Exception ex)
            {
                FileOpetation.SaveRecord(DateTime.Now.ToString() + ": 推送失败 " + ex.StackTrace);
                return false;
            }

        }
    }
}
